<%@ page contentType="text/html;charset=utf-8"%>
<%@ page
	import="java.io.*, java.sql.*, java.util.*, java.text.*, napal.*, util.*"%>
<%@ page import="org.apache.ibatis.*"%>
<%
	request.setCharacterEncoding("utf-8");
%>
<%
	String cmd = request.getParameter("cmd");
String selectedCategory = request.getParameter("category");
String userDepartNumber = (String)session.getAttribute("userDepart");

String model_id = request.getParameter("model_id");
String str_aid = request.getParameter("str_aid");
String e_sto = request.getParameter("e_sto");

int mid = Integer.parseInt(model_id);//모델번호

String m_name = request.getParameter("m_name");//모델명
String m = (String)session.getAttribute("managerID");
String u = (String)session.getAttribute("userID");
String uRank = (String)session.getAttribute("userRank");
int rState=0;
int wState=0;
int oState=0;
int getDepart=0;
int userDepart=0;
int checkc=0;


if (RentalHome.CheckC().isEmpty()==false) {
	checkc=1; }

if(u!=null) {
	if (request.getParameter("rentalday") != null
			&& request.getParameter("rentalvalue") != null
			&& request.getParameter("useStatus") !=null) {

		
		String rentalDay = request.getParameter("rentalday");
		int day = Integer.parseInt(rentalDay);//대여기간
		String rentalValue = request.getParameter("rentalvalue");//대여분류
		String useStatus =request.getParameter("useStatus");//보관사용처
		String rentalmemo = request.getParameter("rentalmemo");//대여사유

		if (RentalHome.RentalExist(u) != null) {
			for (Rental r : RentalHome.RentalExist(u)) {
				rState = r.get대여번호();
			}}
		if(RentalHome.RentalWaitExist(u) != null){
			for(RentalWait w : RentalHome.RentalWaitExist(u)){
				wState = w.get대여대기번호();
			}
		}
		if(RentalHome.OverdueExist(u) != null){
			for(Overdue o : RentalHome.OverdueExist(u)){
				oState = o.get연체번호();
			}
		}
		if(request.getParameter("useStatus")!=null){
			for(Equipment e : RentalHome.getModelDepart(request.getParameter("useStatus"))){
				getDepart = e.get학과번호();//기자재의학과번호
			}
		}
		if(RentalHome.getDepart(userDepartNumber)!=null){
			for(Department d : RentalHome.getDepart(userDepartNumber)){
				userDepart = d.get학과번호();//사용자의학과번호
			}
		}

if(rState!=0){
%>

<script language=javascript>
	self.window.alert("이미 대여중인 기자재가 있습니다 . ");
	location.href = "./RentalList_s.jsp";
</script>
<%
}
else if(checkc==1){
%>

<script language=javascript>
	self.window.alert("정기점검 중입니다(대여불가)");
	location.href = "./RentalList_s.jsp";
</script>
<%

}

else if(wState!=0){
%>

<script language=javascript>
	self.window.alert("이미 대여신청중인 기자재가 있습니다 . ");
	location.href = "./RentalList_s.jsp";
</script>
<%

}
else if(oState!=0){
%>

<script language=javascript>
	self.window.alert("연체기록이 있어서 대여가 불가합니다 . ");
	location.href = "./RentalList_s.jsp";
</script>
<%

}
else if(uRank.equals("휴학생")){
%>

<script language=javascript>
	self.window.alert("휴학생은 대여가 불가합니다 . ");
	location.href = "./RentalList_s.jsp";
</script>

<%


	}

else if(getDepart!=0&&userDepart!=0){
	if ("대여신청".equals(cmd)) {
		List<book> b=RentalHome.getbook3(e_sto, mid);
		if(b.isEmpty()==false){
			 for(book b2: RentalHome.getbook3(e_sto, mid)){
				for(EM b3 : RentalHome.getModelCount(mid,e_sto)){
				 	if(b3.get대여가능수()==1&&u.equals(b2.get사용자ID())){
											 if(getDepart==userDepart){//기자재의 학과와 사용자의 학과가 동일하다면
								RentalHome.insertRentalWait(mid, u, day, rentalValue, useStatus, rentalmemo, 1);//대여과정을 한단계 건너뜀(학과장승인건너뜀)
									 %>
<script language=javascript>
	self.window.alert("기자재를 대여신청하였습니다.");
	location.href = "./RentalList_s.jsp";
</script>
<%
									 }
							else{
								RentalHome.insertRentalWait(mid, u, day, rentalValue, useStatus, rentalmemo, 0);
							%>
<script language=javascript>
	self.window.alert("기자재를 대여신청하였습니다.");
	location.href = "./RentalList_s.jsp";
</script>
<% }
				 	}
					 
				 	else if(b3.get대여가능수()==1&&!u.equals(b2.get사용자ID())){%>
				 	<script language=javascript>
	self.window.alert("기자재 예약이 되어있습니다.(예약자가 대여하기 전 까지 대여불가)");
	location.href = "./RentalList_s.jsp";
</script>

		<%			 }
					 
				 	else{
				 		if(getDepart==userDepart){//기자재의 학과와 사용자의 학과가 동일하다면
							RentalHome.insertRentalWait(mid, u, day, rentalValue, useStatus, rentalmemo, 1);//대여과정을 한단계 건너뜀(학과장승인건너뜀)
								 %>
<script language=javascript>
self.window.alert("기자재를 대여신청하였습니다.");
location.href = "./RentalList_s.jsp";
</script>
<%
								 }
						else{
							RentalHome.insertRentalWait(mid, u, day, rentalValue, useStatus, rentalmemo, 0);
						%>
<script language=javascript>
self.window.alert("기자재를 대여신청하였습니다.");
location.href = "./RentalList_s.jsp";
</script>
<% }	
				 	}
					}
					}
				 }
		else{
				if(getDepart==userDepart){//기자재의 학과와 사용자의 학과가 동일하다면
			RentalHome.insertRentalWait(mid, u, day, rentalValue, useStatus, rentalmemo, 1);//대여과정을 한단계 건너뜀(학과장승인건너뜀)
				 %>
<script language=javascript>
self.window.alert("기자재를 대여신청하였습니다.");
location.href = "./RentalList_s.jsp";
</script>
<%
				 }
		else{
			RentalHome.insertRentalWait(mid, u, day, rentalValue, useStatus, rentalmemo, 0);
		%>
<script language=javascript>
self.window.alert("기자재를 대여신청하였습니다.");
location.href = "./RentalList_s.jsp";
</script>
<%				 }

			}
		}
	}
	}
}
%>

<%@ include file="./Header.jsp"%>
</td>
</TR>
<TR HEIGHT="100">
	<td><a href="RentalList_s.jsp" class="btn btn-link btn-small">대여신청<br></a>
		> 기자재상세정보 > 대여신청 <br>
	<br>
	<br>
		<form>
			<%
				if (RentalHome.RentalExist(u) != null) {
					for (Rental r : RentalHome.RentalExist(u)) {
			%>
			<input type="hidden" name="rentalState" value="<%=r.get대여번호()%>">

			<%
				}
				}
			%>
		</form>
		<form onsubmit="return confirm('해당 기자재를 대여신청하시겠습니까?'); ">

			<p align="center">
			<table width=400, height=100, class="table">
				<tr>
					<td width=20%><p align="right">
							<b>대여자ID</b>
						</p></td>
					<td width=80%><p align="center">
							<%
								if (u != null) {
							%>
							<%=(String) session.getAttribute("userID")%>
							<%
								}
							%>

						</p></td>
					
				</tr>

				<tr>
					<td width=20%><p align="right">
							<b>대여자이름</b>
						</p></td>
					<td width=80%><p align="center">
							<%
								if (u != null) {
							%>
							<%=(String) session.getAttribute("userName")%>
							<%
								}
							%>

						</p></td>
						
				</tr>

				<tr>
					<td width=20%><p align="right">
							<b>대여자소속학과</b>
						</p></td>
					<td width=80%><p align="center">
							<%
								if (u != null) {
							%>
							<%=(String) session.getAttribute("userDepart")%>
							<%
								}
							%>

						</p></td>
						
				</tr>
				<tr>
					<td width=20%><p align="right">
							<b>대여모델명</b>
						</p></td>
					<td width=80%><p align="center">
							<%=m_name%>
						</p></td>
					
				</tr>

				<tr>

					<Td><p align="right">
							<b>대여기간</b>
						</p></Td>
					<td><p align=center>
							<input type="text" name="rentalday">일
						<br></p>
						<p align=center style="color : gray;">기본 : 실습용(7일/30일)&nbsp;&nbsp;인도창(365일)&nbsp;&nbsp;프로젝트(100일)&nbsp;&nbsp;연구용(100일(한학기))</p></td>
				</tr>
				<tr>
					<td><p align="right">
							<b>대여분류</b>
						</p></td>
					<td><p align=center>
							<%if(uRank.equals("재학생")){ %><input
								type="radio" name="rentalvalue" value="실습용">실습용
							&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="rentalvalue"
								value="연구용" disabled="disabled">연구용
							<%} else if(uRank.equals("교수")||uRank.equals("학과장")||uRank.equals("외래교수")){	%>
							<input type="radio" name="rentalvalue" value="실습용"
								disabled="disabled">실습용 &nbsp;&nbsp;&nbsp;&nbsp;<input
								type="radio" name="rentalvalue" value="연구용">연구용
							<%} %>
						</p></td>
						
				</tr>
				<tr>
					<td><p align="right">
							<b>대여사유</b>
						</p></td>
					<td><p align=center>
							<textarea name="rentalmemo" style="width: 70%" rows=6
								class="input-block-level"></textarea>
						</p></td>
						
				</tr>
			</table>
			</p>
			<p align="center">
				<a href="RentalList_s.jsp" name="cmd" class="btn btn-inverse">목록으로</a>
				<input name="cmd" class="btn btn-inverse" type=submit value="대여신청">
			</p>

			<%
		if(str_aid!=null){
			for(Equipment e : RentalHome.getUseStatus(Integer.parseInt(str_aid))){
				 
				%>
			<input type="hidden" name="useStatus" value="<%=e.get보관사용처() %>">
			<%
			}
			
		}
		%>
			<input type=hidden name="model_id" value="<%=model_id%>">
			<input type="hidden" name="e_sto" value="<%=e_sto%>" >
		</form></td>
</tr>
<TR HEIGHT="50">
	<TD><%@ include file="./bottom.jsp"%></TD>
</TR>
</TABLE>
</body>
</html>